/**
 * 
 */
package com.mnurre.shopping.assistant;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;

/**
 * @author michael
 *
 */
public class EditListActivity extends ListActivity {

	private static final String TAG = "EditListActivity";
	
	private EditText editListNameEditText;
	
	private ListsData mData;
//	private Cursor mListCursor;
//	private Cursor mProductsCursor;
	private Long mListId;
//	private Long mSelectedProductId;
	
	private static final int PRODUCT_ADD = 0;
	private static final int PRODUCT_EDIT = 1;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Creating activity.");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_list);
		
		Log.d(TAG, "Getting UI components.");
		editListNameEditText = (EditText)findViewById(R.id.editListNameEditText);
		
		Log.d(TAG, "Creating a new instance of ListsData.");
		mData = new ListsData(this);
		
		Log.d(TAG, "Registering for context menu.");
		registerForContextMenu(getListView());
		
		Log.d(TAG, "Checking to see if we are editing.");
		Bundle extras = getIntent().getExtras();
		if(extras != null){
			Log.d(TAG, "We are editing a list.");
			setTitle(R.string.list_edit_activity_label);
			mListId = extras.getLong(DataManager.Tables.Lists.COLUMN_ID);
			Log.d(TAG, "Edited list id is " + mListId);
			Log.d(TAG, "Calling populate fields.");
			populateFields();
		} else {
			Log.d(TAG, "We are adding a new list.");
			setTitle(R.string.list_add_activity_label);
			mListId = null;
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Creating options menu.");
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_edit_list, menu);
		Log.d(TAG, "Done creating options menu.");
		return true;
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Creating context menu.");
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_manage_context, menu);
		Log.d(TAG, "Done creating context menu.");
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		// refresh the list
		populateFields();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Content menu item selected.");
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		switch(item.getItemId()){
		case R.id.manage_menu_context_option_delete:
			Log.d(TAG, "Delete context menu selcted.");
			mData.open();
			mData.removeProductFromList(mListId, info.id);
			mData.close();
			populateFields();
			break;
		case R.id.manage_menu_context_option_edit:
			Log.d(TAG, "Edit context menu selected.");
			Intent intent = new Intent(this, EditProductActivity.class);
			intent.putExtra(DataManager.Tables.Products.COLUMN_ID, info.id);
			if(mListId == null){
				saveList();
			}
			startActivityForResult(intent, PRODUCT_EDIT);
			break;
		}
		return super.onContextItemSelected(item);
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem)
	 */
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch(item.getItemId()){
		case R.id.menu_manage_add:
			Log.d(TAG, "The add menu was selected. Starting intent to add product.");
			intent = new Intent(this, ManageProductsActivity.class);
			// if we have a new list, we will not have a list id yet so save it first
			if(mListId == null){
				saveList();
			}
			intent.putExtra(DataManager.Tables.Lists.COLUMN_ID, mListId);
			startActivityForResult(intent, PRODUCT_ADD);
			break;
		case R.id.menu_manage_save:
			Log.d(TAG, "The save menu was selected. Saving list.");
			saveList();
			setResult(RESULT_OK);
			finish();
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}
	
	private void saveList(){
		Log.d(TAG, "saveList called.");
		mData.open();
		mListId = mData.saveList(editListNameEditText.getText().toString());
		mData.close();
		Log.d(TAG, "Done saving list. ListId is " + mListId);
	}

	private void populateFields() {
		// TODO Auto-generated method stub
		Log.d(TAG, "populateFields called. Opening data and managing cursor.");
		mData.open();
		Cursor listCursor = mData.getListById(mListId);
		startManagingCursor(listCursor);
		Log.d(TAG, "Setting the list name text box.");
		editListNameEditText.setText(listCursor.getString(listCursor.getColumnIndex(DataManager.Tables.Lists.COLUMN_NAME)));
		Log.d(TAG, "Getting a products cursor for products in list.");
		Cursor productCursor = mData.getProductsInList(mListId);
		startManagingCursor(productCursor);
		Log.d(TAG, "Creating from array.");
		String[] from = new String[] { DataManager.Tables.Products.COLUMN_CATEGORY,
				DataManager.Tables.Products.COLUMN_NAME,
				DataManager.Tables.Products.COLUMN_DESCRIPTION,
				DataManager.Tables.Products.COLUMN_PRICE };
		Log.d(TAG, "Creating to array.");
		int[] to = new int[] { R.id.productItemCategoryTextView,
				R.id.productItemNameTextView,
				R.id.productItemDescriptionTextView,
				R.id.productItemPriceTextView };
		Log.d(TAG, "Creating a new SimpleCursorAdapter.");
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.product_item_table_layout, productCursor, from, to);
		Log.d(TAG, "Calling setListAdapter.");
		setListAdapter(adapter);
		Log.d(TAG, "Closing data connection.");
		mData.close();
	}
}
